<template>
  <div class="register-container">
    <el-form ref="form" :rules="rules" class="register-form" :model="formData" label-width="80px">
      <h3>注册</h3>
      <el-form-item prop="avatar" label="头像">
        <el-upload action="/user/upload" name="avatar" class="avatar-container" :on-success="hadleSuccess" :show-file-list="false">
          <img v-if="imgUrl" :src="imgUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="account" label="账号">
        <el-input v-model="formData.account" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item prop="userName" label="用户名">
        <el-input v-model="formData.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="formData.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="password2" label="确认密码">
        <el-input v-model="formData.password2" type="password" placeholder="请再次输入密码"></el-input>
      </el-form-item>
      <el-button type="primary" class="register-btn" @click="register">注册</el-button>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'Register',
  data() {
    const validatePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 3) {
        callback(new Error('密码长度不能小于3'))
      } else {
        if (this.formData.password2 !== '') {
          this.$refs.form.validateField('password2')
        }
        callback()
      }
    }

    const validatePwd2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value.length < 3) {
        callback(new Error('密码长度不能小于3'))
      } else if (value !== this.formData.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      imgUrl: '',
      formData: {
        avatar: '',
        account: '',
        userName: '',
        password: '',
        password2: ''
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, validator: validatePwd, trigger: 'blur' }],
        password2: [{ required: true, validator: validatePwd2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    hadleSuccess(res, file) {
      this.imgUrl = res.data
      this.formData.avatar = res.data
    },
    register() {
      this.$refs.form.validate((valid) => {
        const self = this
        if (valid) {
          register(this.formData).then((res) => {
            if (res.errcode === 0) {
              this.$message({
                message: '恭喜你，注册成功',
                type: 'success',
                duration: 1500,
                onClose() {
                  self.$router.push('/login')
                }
              })
            } else {
              this.$message({
                message: res.errmsg,
                type: 'warning'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less">
.avatar-container {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  &:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
  .avatar {
    width: 148px;
    width: 148px;
    display: block;
  }
}
</style>
<style lang="less" scoped>
.register-container {
  background-color: #fff;
  padding: 20px;
  width: 600px;
  margin: 0 auto;
  position: relative;
  top: 145px;
  box-shadow: 0 0 10px 10px rgba(0, 0, 0, 0.1);
  h3 {
    text-align: center;
    margin: 0 0 20px 0;
  }
  .register-btn {
    width: 480px;
    margin-left: 80px;
  }
}
</style>
